

Page({

  data:{
    name:'',
    id:'',
    password:'',
    isSigned:'',
    // finish:  false,
  },
  onShow(){
    this.setData({
      isSigned: false,
      // finish: false
    })
  },
  getName(e){
    console.log("获取用户名",e.detail.value)
    this.setData({
      name:e.detail.value
    })
  },
  getID(e){
    console.log("获取账号",e.detail.value)
    this.setData({
      id:e.detail.value
    })
  },
  getPassword(e){
    console.log("获取密码",e.detail.value)
    this.setData({
      password:e.detail.value
    })
  },

  SignUp()
  {
    let name = this.data.name
    let id = this.data.id
    let password = this.data.password

    console.log("注册点击")
    console.log("name", name)
    console.log("ID", id)
    console.log("密码", password)

    if(name.length < 4){
      wx.showToast({
        title: '用户名长度至少为4个字符',
        icon: 'none'
      })
      return 
    }
    if(name.length > 12){
      wx.showToast({
        title: '用户名长度至多为12个字符',
        icon: 'none'
      })
      return
    }
    if(id.length != 8){
      wx.showToast({
        title: 'id长度必须为8个字符',
        icon: 'none'
      })
      return
    }
    if(password.length < 6){
      wx.showToast({
        title: '密码长度至少为6个字符',
        icon: 'none'
      })
      return
    }
    wx.cloud.database().collection('user').where({
      id : id
    }).get().then(res=>{
      console.log(res.data.length)
      if (res.data.length > 0) {
        this.setData({
          isSigned: true
        })
        console.log("isSigned1", this.data.isSigned)
      }
      
      // this.setData({
      //     finish: true
      // })
    })

    // this.setData({
    //   finish: false
    // })

    setTimeout(() => {
      console.log("isSigned2", this.data.isSigned)
      if(this.data.isSigned) {
        console.log("用户被注册了")
        wx.showToast({
          title: '该ID已被注册',
          icon: 'none'
        })

        this.setData({
          isSigned: false
        })
        return
      }
      wx.showLoading({
        title: '正在注册中',
      })
      wx.cloud.database().collection('user').add({
        data: {
          name:name,
          id:id,
          pasword:password
        },
        success(res){
          console.log("注册成功", res)
          wx.showToast({
            title: '注册成功',
          })
          wx.navigateTo({
            url: '../login/login',
          })          
        },
        fail(res){
          console.log("注册失败", res)
        }
      })
    }, 1000)
  }
})